import {
    GET
} from './utils/methods';

App({
   
    onLaunch: function (query) {
        const app = this;
        // 展示本地存储能力
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        // 登录
        if (query.query.deptId) {
            this.globalData.codeDeptId = query.query.deptId;
            this.globalData.deptId = query.query.deptId;

        }
        this.login(app);
        // 获取用户信息
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: res => {
                            // 可以将 res 发送给后台解码出 unionId
                            this.globalData.userInfo = res.userInfo
                            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                            // 所以此处加入 callback 以防止这种情况
                            if (this.userInfoReadyCallback) {
                                this.userInfoReadyCallback(res)
                            }
                        }
                    })
                }
            }
        })

        /**
         * 获取当前的机型如果是 iPhone X 或以上机型
         * （ 则空余出底部空间 ）
         */
        wx.getSystemInfo({
            success: (result) => {
                app.globalData.is_iphonex = result.model.indexOf('iPhone X') > -1
            },
        })
    },

    onShow: function (query) {
        console.log('query',query)
        const app = this;
        console.log(app)

  
        wx.checkSession({
            fail() {
                app.login(app)
            }
        });
    },
    login: function (app) {
        console.log(5)
        let that = this
        console.log('that.globalData.retryTime',that.globalData.retryTime)
        if(that.globalData.retryTime<3){
            return new Promise(function (resolve, reject) {
                wx.login({
                    success: res => {
                        // 发送 res.code 到后台换取 openId, sessionKey, unionId
                        if (res.code) {
                            that.globalData.code = res.code
                            wx.request({
                                url: app.getUrl("wechat/oauth/miniLogin"), //仅为示例，并非真实的接口地址
                                method: "GET",
                                data: {
                                    grant_type: "authorization_code",
                                    alias: app.globalData.config.alias,
                                    code: res.code,
                                    deptId: app.globalData.deptId
                                },
                                success: res => {
                                    if (res.data.code == 0) {
                                        // app.globalData.deptId = 'gdsdermyydept001'
                                        if (res.data.data.ptDefaultDeptId) {
                                            app.globalData.deptId = res.data.data.ptDefaultDeptId
                                        }
                                        // app.globalData.queueId = "gdsdermyy12queue001"
                                        app.globalData.queueId = res.data.data.ptDefaultQueueId;
                                        var header = {};
                                        header.Cookie = 'JSESSIONID=' + res.data.data.sessionId;
                                        wx.setStorage({
                                            data: true,
                                            key: 'already_cookie'
                                        })
                                        wx.setStorageSync("cookie", header);
                                        app.globalData.userId = res.data.data.ptDefaultUserId;
                                        app.globalData.userMobile = res.data.data.mobile;
                                        app.globalData.account = res.data.data.ptAccountId;
                                        app.globalData.openid = res.data.data.openid;
                                        wx.request({
                                            url: app.getUrl('/xcx/pt/getUsersInfo'),
                                            method: 'GET',
                                            header: wx.getStorageSync('cookie'),
                                            success(res) {
                                                if (res.data.code == 0 || res.data.code == 200) {
                                                    app.globalData.userList = res.data.data
                                                }
                                            }
                                        })
                                        if (!res.data.data.miniBackEndUrl && !app.globalData.forwarded) {
                                            app.globalData.config.server = app.globalData.config.oriServer;
                                            app.globalData.forwarded = true;
                                            app.globalData.definedUrl = true;
                                            setTimeout(() => app.login(app), 1000);
                                        } else if (res.data.data.miniBackEndUrl && res.data.data.miniBackEndUrl != app.globalData.config.server) {
                                            app.globalData.definedUrl = false;
                                            app.globalData.forwarded = true;
                                            app.globalData.config.server = res.data.data.miniBackEndUrl;
                                            setTimeout(() => app.login(app), 1000);
                                        } else {
                                            app.globalData.forwarded = true;
                                            app.globalData.definedUrl = true;
                                        }
                                        resolve(res.data.data.sessionId)
                                    } else {
                                        // setTimeout(() => app.login(app), 1000);
                                        that.globalData.retryTime++
                                        app.login(app)
                                    }
                                }
                            })
                        }
                    }
                })
            })
        }else{
            wx.showToast({
                title: '网络连接异常，请稍后重试',
                icon: 'none'
            })
        }
     
    },
  
    // refresh: function () {
    //     var that = this;
    //     const app = this;
    //     setInterval(function () {
    //         wx.checkSession({
    //             fail() {
    //                 wx.login({
    //                     success: res => {
    //                         // 发送 res.code 到后台换取 openId, sessionKey, unionId
    //                         if (res.code) {
    //                             that.globalData.code = res.code
    //                             wx.request({
    //                                 url: app.getUrl("wechat/oauth/miniLogin"), //仅为示例，并非真实的接口地址
    //                                 method: "GET",
    //                                 data: {
    //                                     grant_type: "authorization_code",
    //                                     alias: app.globalData.config.alias,
    //                                     code: res.code
    //                                 },
    //                                 success: res => {
    //                                     if (res.data.code == 0) {
    //                                         if (res.data.data.ptDefaultDeptId) {
    //                                             app.globalData.deptId = res.data.data.ptDefaultDeptId
    //                                         }
    //                                         // app.globalData.queueId = res.data.data.ptDefaultQueueId;
    //                                         var header = {};
    //                                         header.Cookie = 'JSESSIONID=' + res.data.data.sessionId;
    //                                         wx.setStorageSync("cookie", header);
    //                                         // app.globalData.userId = res.data.data.ptDefaultUserId;
    //                                         app.globalData.userMobile = res.data.data.mobile;
    //                                         app.globalData.account = res.data.data.ptAccountId;
    //                                         app.globalData.openid = res.data.data.openid;
    //                                         wx.request({
    //                                             url: app.getUrl('/xcx/pt/getUsersInfo'),
    //                                             method: 'GET',
    //                                             header: wx.getStorageSync('cookie'),
    //                                             success(res) {
    //                                                 if (res.data.code == 0 || res.data.code == 200) {
    //                                                     app.globalData.userList = res.data.data
    //                                                 }
    //                                             }
    //                                         })
    //                                         if (!res.data.data.miniBackEndUrl && !app.globalData.forwarded) {
    //                                             app.globalData.config.server = app.globalData.config.oriServer;
    //                                             app.globalData.forwarded = true;
    //                                             app.globalData.definedUrl = true;
    //                                             // setTimeout(() => app.login(app), 1000);
    //                                         } else if (res.data.data.miniBackEndUrl && res.data.data.miniBackEndUrl != app.globalData.config.server) {
    //                                             app.globalData.definedUrl = false;
    //                                             app.globalData.forwarded = true;
    //                                             app.globalData.config.server = res.data.data.miniBackEndUrl;
    //                                             setTimeout(() => app.login(app), 1000);
    //                                         } else {
    //                                             app.globalData.forwarded = true;
    //                                             app.globalData.definedUrl = true;
    //                                         }
    //                                     } else {
    //                                         setTimeout(() => app.login(app), 1000);
    //                                     }
    //                                 }
    //                             })
    //                         }
    //                     }
    //                 });
    //             }
    //         })
    //     }, 10 * 60 * 500);
    // },
    getUrl: function (url) {
        if (!url.startsWith('/')) {
            url = "/" + url
        }
        return this.globalData.config.server + url;
    },
    // 请求量表接口时使用
    getWebUrl: function (url) {
        if (!url.startsWith('/')) {
            url = "/" + url
        }
        return this.globalData.config.webServer + url;
    },
    getEducationUrl: function (url) {
        if (!url.startsWith('/')) {
            url = "/" + url
        }
        return this.globalData.config.educationServer + url;
    },
    //书写全局变量
    globalData: {
        code: null,
        currentPatient: null,
        hospitalInfo:null,
        userInfo: null,
        userId: null,
        queueId: null,
        userMobile: null,
        deptId: null,
        openid: null,
        codeDeptId: null,
        forwarded: false,
        tabIndex:0,
        statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
        header: {
            'Cookie': ''
        },
        userList: null,
        safe_bottom: 0,
        retryTime: 0,
        /**
         * 云版 正式环境 接口地址
         * 新生儿
         */
        //  config: {
        //   alias: "prod",
        //   server: "https://xcx.bb-care.com/bbCare",
        //   oriServer: "https://xcx.bb-care.com/bbCare",
        //   educationServer: "https://xcx.bb-care.com/bbCare",
        //   webServer: "https://xcx.bb-care.com"
        // },
        // 部署
        // config: {
        //   alias: "earlyChildhood",
        //   server: 'https://xcx.bb-care.com/earlyChildhood',
        //   oriServer: 'https://xcx.bb-care.com/earlyChildhood',
        //   educationServer: 'https://xcx.bb-care.com/earlyChildhood',
        //   archiveServer: 'https://xcx.bb-care.com/earlyChildhood'
        // },

        // config: {
        //   alias: "Qmx",
        //   server: "https://xcx.bb-care.com/qmx",
        //   oriServer: "https://xcx.bb-care.com/qmx",
        //   educationServer: "https://xcx.bb-care.com/qmx"
        // },

        /**
         * 云版测试环境 接口地址
         */
        // config: {
        //   alias: "dev",
        //   server: 'https://xcxtest.bb-care.com/bbCare',
        //   oriServer: 'https://xcxtest.bb-care.com/bbCare',
        //   educationServer: 'https://xcxtest.bb-care.com/bbCare',
        //   archiveServer: 'https://xcxtest.bb-care.com/platform',
        //   webServer: "https://xcxtest.bb-care.com"
        // },
        //  config: {
        //     alias: "dev",
        //     server: "https://xcx.bb-care.com/earlyChildhood",
        //     oriServer: "https://xcx.bb-care.com/earlyChildhood",
        //     educationServer: "https://xcx.bb-care.com/earlyChildhood",
        //     webServer: "https://xcx.bb-care.com"
        // },
       // 早发（凯里、西安、新华、浠水、单病种、凯里1） 

        // config: {
        //     alias: "earlyChildhood",
        //     server: "https://xcx.bb-care.com/earlyChildhood",
        //     oriServer: "https://xcx.bb-care.com/earlyChildhood",
        //     educationServer: "https://xcx.bb-care.com/earlyChildhood",
        //     webServer: "https://xcx.bb-care.com"
        // },
        config: {
          alias: "earlyChildhood",
          server: "http://192.168.1.199/earlyChildhood",
          oriServer: "http://192.168.1.199/earlyChildhood",
          educationServer: "http://192.168.1.199/earlyChildhood",
          webServer: "http://192.168.1.199"
      },
         /** * 本地环境 接口地址 
         */
        // config: {
        //   alias: "prod",
        //   server: 'http://192.168.3.166:8081/bbcareDeploy',
        //   oriServer: 'http://192.168.3.166:8081/bbcareDeploy',
        //   educationServer: 'http://192.168.3.166:8081/bbcareDeploy',
        //   archiveServer: 'http://192.168.3.166:8081/bbcareDeploy'
        // }

        // hu
        // config: {
        //   alias: "earlyChildhood",
        //   server: 'http://192.168.3.141:8081/bbcareDeploy',
        //   oriServer: 'http://192.168.3.141:8081/bbcareDeploy',
        //   educationServer: 'http://192.168.3.141:8081/bbcareDeploy',
        //   archiveServer: 'http://192.168.3.141:8081/bbcareDeploy'
        // }

        // config: {
        //     alias: "prod",
        //     server: 'http://192.168.3.166/bbcareDeploy',
        //     oriServer: 'http://192.168.3.166/bbcareDeploy',
        //     educationServer: 'http://192.168.3.166/bbcareDeploy',
        //     archiveServer: 'http://192.168.3.166/bbcareDeploy'
        //   }
        // config: {
        //     alias: "earlyChildhood",
        //     server: "http://192.168.3.166:8081/bbcareDeploy",
        //     oriServer: "http://192.168.3.166:8081/bbcareDeploy",
        //     educationServer: "http://192.168.3.166:8081/bbcareDeploy",
        //     webServer: "http://xcx.bb-care.com"
        // },
         
        //启明星
        // config: {
        //     alias: "Qmx",
        //     server: 'http://192.168.3.166:8081/qmx',
        //     oriServer: 'http://192.168.3.166:8081/qmx',
        //     educationServer: 'http://192.168.3.166:8081/qmx',
        //     archiveServer: 'http://192.168.3.166:8081/qmx'
        //   }
    }
})

const app = getApp();
export default app;